package com.wing.oauth2.details.userdetails.member;

import com.wing.common.enums.AuthMethodEnum;
import com.wing.common.utils.JsonResult;
import com.wing.member.feign.MemberClient;
import com.wing.member.model.response.MemberAuthVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 会员用户认证服务
 *
 * @author
 */
@Service("memberUserDetailsService")
@RequiredArgsConstructor
public class MemberUserDetailsServiceImpl implements UserDetailsService {

    private final MemberClient memberClient;

    /**
     * 用户名加载用户信息
     *
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        JsonResult<MemberAuthVO> memberResult = memberClient.findMemberByUsername(username);
        if (memberResult == null || memberResult.getData() == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return this.buildUserDetails(memberResult.getData(), AuthMethodEnum.USERNAME);
    }

    public UserDetails loadUserByMobile(String phone) {
        JsonResult<MemberAuthVO> memberResult = memberClient.findMemberByPhone(Long.parseLong(phone));
        if (memberResult == null || memberResult.getData() == null) {
            throw new UsernameNotFoundException("手机号错误");
        }
        return this.buildUserDetails(memberResult.getData(), AuthMethodEnum.MOBILE);
    }

    /**
     * openid 认证方式
     *
     * @param openId
     * @return
     */
    public UserDetails loadUserByOpenId(String openId) {
        JsonResult<MemberAuthVO> memberResult = memberClient.findMemberByOpenId(openId);
        if (memberResult == null || memberResult.getData() == null) {
            throw new UsernameNotFoundException("openId错误");
        }
        return this.buildUserDetails(memberResult.getData(), AuthMethodEnum.OPENID);
    }

    /**
     * 构建用户详细信息
     *
     * @param user 系统用户
     * @return 用户详细对象
     */
    private UserDetails buildUserDetails(MemberAuthVO user, AuthMethodEnum methodEnum) {
        
        MemberUserDetails userDetails = new MemberUserDetails(user);
        userDetails.setAuthMethod(methodEnum.getValue());

        if (!userDetails.isEnabled()) {
            throw new DisabledException("该账户已被禁用!");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("该账号已被锁定!");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("该账号已过期!");
        }
        
        return userDetails;
    }
}
